#include "service/include/swei_cmd.h"
#include "service/include/swei_downlink.h"
#include "comm/co_heartbeat.h"
#include "aadl/public.h"
#include "flash/protocol_params/protocol_params.h"
#include "service/include/swei_config.h"

static DLCMD_RET_TYPE_ENUM tbox_audio_handler(DOWN_LINK_CMD_ENUM cmd_id, uint16_t cmd_opcode, const unsigned char * p_data, uint32_t data_len);

DOWN_LINK_CMD_ENUM aud_tbl[] = {
    DOWN_LINK_CMD_SET_RIDE_ABNORMAL_SOUND,               
    DOWN_LINK_CMD_SOUND_STATUS,
    DOWN_LINK_CMD_SET_POWER_ON_SOUND,
    DOWN_LINK_CMD_SET_POWER_OFF_SOUND,
    DOWN_LINK_CMD_SET_FIND_BIKE_SOUND, 
    DOWN_LINK_CMD_SET_CHARGER_LINK_SOUND,  
    DOWN_LINK_CMD_SET_CHRISTMAS_SOUND, 
    DOWN_LINK_CMD_SET_BIRTHDAY_SOUND,   
    DOWN_LINK_CMD_SET_NEW_YEAR_SOUND,
    DOWN_LINK_CMD_SET_LOW_POWER_SOUND,
    DOWN_LINK_CMD_SET_BARRERY_ABNORMAL_SOUND,
    DOWN_LINK_CMD_SET_SHIFT_SOUND,
    DOWN_LINK_CMD_SET_MOTOR_SUPERSONIC_SOUND,
    DOWN_LINK_CMD_SET_TBOX_BIND_SOUND,
    DOWN_LINK_CMD_SET_IN_ELE_FANCE_SOUND,
    DOWN_LINK_CMD_SET_THANKSGIVING_SOUND,
    DOWN_LINK_CMD_VOLUME,
    DOWN_LINK_CMD_FENCE_ALARM,
};

static aud_cfg_t aud_cfg = {0};
static platform_config_param_st plat_conf;

void swei_audio_init(void) 
{
    for (int i = 0; i < ARRAY_COUNT(aud_tbl); i++) {
        swei_downlink_cmd_register(aud_tbl[i], tbox_audio_handler);
    }

    /* 读取平台参数 */
    config_get(CONFIG_ID_PLAT, &plat_conf);
    aud_cfg.aud_caralarm = plat_conf.common.ride_abnormal_sound.status;
    aud_cfg.aud_switch = plat_conf.common.sound_status;
    aud_cfg.aud_poweron = plat_conf.common.power_on_sound.status;
    aud_cfg.aud_poweroff = plat_conf.common.power_off_sound.status;
    aud_cfg.aud_findbike = plat_conf.common.find_bike_sound.status;
    aud_cfg.aud_bat_low = plat_conf.common.low_power_sound;
    aud_cfg.aud_bat_err = plat_conf.common.battery_abnormal_sound;
    aud_cfg.aud_shift_gear = plat_conf.common.shift_sound;
    aud_cfg.aud_ovr_spd = plat_conf.common.motor_supersonic_sound;
    aud_cfg.aud_ble_band = plat_conf.common.ble_match_sound;
    aud_cfg.aud_charger_in = plat_conf.common.charger_link_sound.status;
    aud_cfg.aud_fence_in = plat_conf.common.in_ele_fance_sound; 
    aud_cfg.aud_date_mc = plat_conf.common.christmas_sound.status;
    aud_cfg.aud_date_tg = plat_conf.common.thanksgiving_sound.status;
    aud_cfg.aud_date_bd = plat_conf.common.birthday_sound.status;
    aud_cfg.aud_date_ny = plat_conf.common.new_year_sound.status;
}

static DLCMD_RET_TYPE_ENUM tbox_audio_handler(DOWN_LINK_CMD_ENUM cmd_id, uint16_t cmd_opcode, const unsigned char * p_data, uint32_t data_len)
{
    if (p_data == NULL)      
        return DLCMD_RET_ERROR;

    int volume = 0;
    int type = 0;
    
    switch (cmd_id)
    {
        /* 震动或骑行异常报警音设置 0：音效1 / 1：音效2 / 2 自定义 */
        case DOWN_LINK_CMD_SET_RIDE_ABNORMAL_SOUND:
            /* 音效类型 */
            memcpy(&aud_cfg.aud_caralarm, p_data, 1);
            plat_conf.common.ride_abnormal_sound.status = aud_cfg.aud_caralarm;
            if (data_len != 1)
                strncpy(plat_conf.common.ride_abnormal_sound.url, (char *)p_data + 1, data_len - 1);
            co_aud_cfg(&aud_cfg);
            break;

        /* 音效开关 0 关 / 1 开 */
        case DOWN_LINK_CMD_SOUND_STATUS:
            memcpy(&aud_cfg.aud_switch, p_data, data_len);
            plat_conf.common.sound_status = aud_cfg.aud_switch;
            co_aud_cfg(&aud_cfg);    
            break;

        /* 开机音效设置 0：音效1 / 1：音效2 / 2 自定义 */
        case DOWN_LINK_CMD_SET_POWER_ON_SOUND:
            memcpy(&aud_cfg.aud_poweron, p_data, 1);
            plat_conf.common.power_on_sound.status = aud_cfg.aud_poweron;
            if (data_len != 1)
                strncpy(plat_conf.common.power_on_sound.url, (char *)p_data + 1, data_len - 1);
            co_aud_cfg(&aud_cfg);
            break;

        /* 关机音效设置 0：音效1 / 1：音效2 / 2 自定义 */
        case DOWN_LINK_CMD_SET_POWER_OFF_SOUND:
            memcpy(&aud_cfg.aud_poweroff, p_data, 1);
            plat_conf.common.power_off_sound.status = aud_cfg.aud_poweroff;
            if (data_len != 1)
                strncpy(plat_conf.common.power_off_sound.url, (char *)p_data + 1, data_len - 1);
            co_aud_cfg(&aud_cfg); 
            break;

        /* 寻车声音设置 0：音效1 / 1：音效2 / 2 自定义 */
        case DOWN_LINK_CMD_SET_FIND_BIKE_SOUND:
            memcpy(&aud_cfg.aud_findbike, p_data, 1);
            plat_conf.common.find_bike_sound.status = aud_cfg.aud_findbike;
            if (data_len != 1)
                strncpy(plat_conf.common.find_bike_sound.url, (char *)p_data + 1, data_len - 1);
            co_aud_cfg(&aud_cfg); 
            break;

        /* 电量低提醒设置 0：音效1 / 1：音效2 */
        case DOWN_LINK_CMD_SET_LOW_POWER_SOUND:
            memcpy(&aud_cfg.aud_bat_low, p_data, data_len);
            plat_conf.common.low_power_sound = aud_cfg.aud_bat_low;
            co_aud_cfg(&aud_cfg);
            break;

        /* 电池电控故障声音 0：音效1 / 1：音效2 */
        case DOWN_LINK_CMD_SET_BARRERY_ABNORMAL_SOUND:
            memcpy(&aud_cfg.aud_bat_err, p_data, data_len);
            plat_conf.common.battery_abnormal_sound = aud_cfg.aud_bat_err;
            co_aud_cfg(&aud_cfg);
            break;

        /* 换挡提示音 0：音效1 / 1：音效2 */
        case DOWN_LINK_CMD_SET_SHIFT_SOUND:
            memcpy(&aud_cfg.aud_shift_gear, p_data, data_len);
            plat_conf.common.shift_sound = aud_cfg.aud_shift_gear;
            co_aud_cfg(&aud_cfg); 
            break;

        /* 电机超速音 0：音效1 / 1：音效2 */
        case DOWN_LINK_CMD_SET_MOTOR_SUPERSONIC_SOUND:
            memcpy(&aud_cfg.aud_ovr_spd, p_data, data_len);
            plat_conf.common.motor_supersonic_sound = aud_cfg.aud_ovr_spd;
            co_aud_cfg(&aud_cfg); 
            break;

        /* tbox绑定成功提示音 0：音效1 / 1：音效2 */
        case DOWN_LINK_CMD_SET_TBOX_BIND_SOUND:
            memcpy(&aud_cfg.aud_ble_band, p_data, data_len);
            plat_conf.common.ble_match_sound = aud_cfg.aud_ble_band;
            co_aud_cfg(&aud_cfg);
            break;

        /* 充电器连接成功提示音 0：音效1 / 1：音效2 / 2 自定义 */
        case DOWN_LINK_CMD_SET_CHARGER_LINK_SOUND:
            memcpy(&aud_cfg.aud_charger_in, p_data, 1);
            plat_conf.common.charger_link_sound.status = aud_cfg.aud_charger_in;
            if (data_len != 1)
                strncpy(plat_conf.common.charger_link_sound.url, (char *)p_data + 1, data_len - 1);
            co_aud_cfg(&aud_cfg);
            break;

        /* 进入电子围栏提示音 0：音效1 / 1：音效2 */
        case DOWN_LINK_CMD_SET_IN_ELE_FANCE_SOUND:
            memcpy(&aud_cfg.aud_fence_in, p_data, data_len);
            plat_conf.common.in_ele_fance_sound = aud_cfg.aud_fence_in;
            co_aud_cfg(&aud_cfg); 
            break;

        /* 圣诞提示音 1 人声/2 自定义 */
        case DOWN_LINK_CMD_SET_CHRISTMAS_SOUND:
            memcpy(&aud_cfg.aud_date_mc, p_data, 1);
            plat_conf.common.christmas_sound.status = aud_cfg.aud_date_mc;
            if (data_len != 1)
                strncpy(plat_conf.common.christmas_sound.url, (char *)p_data + 1, data_len - 1);
            co_aud_cfg(&aud_cfg);
            break;

        /* 感恩节提示音 1 人声/2 自定义 */
        case DOWN_LINK_CMD_SET_THANKSGIVING_SOUND:
            memcpy(&aud_cfg.aud_date_tg, p_data, 1);
            plat_conf.common.thanksgiving_sound.status = aud_cfg.aud_date_tg;
            if (data_len != 1)
                strncpy(plat_conf.common.thanksgiving_sound.url, (char *)p_data + 1, data_len - 1);
            co_aud_cfg(&aud_cfg);
            break;

        /* 生日提示音 1 人声/2 自定义 */
        case DOWN_LINK_CMD_SET_BIRTHDAY_SOUND:
            memcpy(&aud_cfg.aud_date_bd, p_data, 1);
            plat_conf.common.birthday_sound.status = aud_cfg.aud_date_bd;
            if (data_len != 1)
                strncpy(plat_conf.common.birthday_sound.url, (char *)p_data + 1, data_len - 1);
            co_aud_cfg(&aud_cfg);
            break;

        /* 新年提示音 1 人声/2 自定义 */
        case DOWN_LINK_CMD_SET_NEW_YEAR_SOUND:
            memcpy(&aud_cfg.aud_date_ny, p_data, 1);
            plat_conf.common.new_year_sound.status = aud_cfg.aud_date_ny;
            if (data_len != 1)
                strncpy(plat_conf.common.new_year_sound.url, (char *)p_data + 1, data_len - 1);
            co_aud_cfg(&aud_cfg);
            break;

        /* 设备TBOX声音大小 1-5 */
        case DOWN_LINK_CMD_VOLUME:
            memcpy(&volume, p_data, data_len);
            plat_conf.common.sound_volume = volume;
            co_volume_cfg(volume);
            break;            

        case DOWN_LINK_CMD_FENCE_ALARM:
            memcpy(&type, p_data, data_len);
            co_aud_cmd(0 == type? AUD_TYPE_ENCLOSURE_IN: AUD_TYPE_ENCLOSURE_OUT);
            break;

        default:
            break;
    }

    save_platform_config(&plat_conf);
    return DLCMD_RET_FINISH;
}